package main

import (
	"fmt"
	"net/http"
	"strings"
	"io/ioutil"
)

func main() {
	req()
	out()
}

func req()  {
	resp, err := http.Post("http://www.baiduss.com", "application/x-www-form-urlencoded", strings.NewReader("id=1"))

	defer func() {
		if err := recover(); err != nil {
			fmt.Println("请求异常：", err)
		}
	}()

	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}

/**
  只有defer recover捕获panic的异常，此方法才会执行
 */
func out()  {
	fmt.Println("发生异常后，新的方法仍会执行")
}
